function loadPromoFactoryList(data) {
//	var startDate = new Date();
//	$("#promoGeneralContentEndDate").val(startDate.getFullYear()+ "/" + (startDate.getMonth()+1)+ "/" + startDate.getDate());
//	startDate.setDate(startDate.getDate()-30);
//	$("#promoGeneralContentStartDate").val(startDate.getFullYear()+ "/" + (startDate.getMonth()+1)+ "/" + startDate.getDate());
//	
	var  selTarget = document.getElementById( "promoFactory" );
	$(data.items).each(function(i, value){
		selTarget.options.add( new  Option(value["name"], value["fid"] ));
	});
	$(".tab-content").show();

	jQuery('#body-wrapper').hideLoading();
}

function getPromoGeneralData() {
	var  selTarget = document.getElementById( "promoFactory" );	
	var startDate = new Date();
	var endDate = new Date();

	if (selTarget.selectedIndex != 0 && $("#promoGeneralContentStartDate").val() != "" && $("#promoGeneralContentEndDate").val() != "")
	{
		jQuery('#body-wrapper').showLoading();
		startDate.setFullYear(($("#promoGeneralContentStartDate").val()).substring(0,4), ($("#promoGeneralContentStartDate").val()).substring(5,7)-1,($("#promoGeneralContentStartDate").val()).substring(8,10));
		endDate.setFullYear(($("#promoGeneralContentEndDate").val()).substring(0,4), ($("#promoGeneralContentEndDate").val()).substring(5,7)-1,($("#promoGeneralContentEndDate").val()).substring(8,10));

		days = parseInt((endDate - startDate)/1000/60/60/24)+1;
		if (days > 0)
		{
			var uname = getCookie("uname"); 
			var pwd = getCookie("pwd");

			var APIurl =  "http://10.103.13.15:8000/client/promoAnalytics?fid="+ selTarget.options[selTarget.selectedIndex].value +"&staDate="+$("#promoGeneralContentStartDate").val()+"&pd="+days + "&uname="+uname + "&pwd="+ pwd;
			$.ajax({
				type: "GET",
				url: APIurl,
				cache: false,
				dataType : "jsonp",
				jsonnp: "callback",
			
				error: function(){
				　　console.log("test");
				},
				success: loadPromoData

			});
		}
		else 
			alert("选择时段区间不对!");
	}
	else if ($("#promoGeneralContentStartDate").val() == "" || $("#promoGeneralContentEndDate").val() == "")
	{
		alert("请选择时段!");
	}
	else if (selTarget.selectedIndex == 0)
	{
		alert("请选择一个渠道");
	}

//	alert(APIurl);

}

var promoNames = new Array(); //pname+pid
var promoDates = new Array();
var promoData = new Array();

function loadPromoData(data) {
	promoNames = new Array();
	promoDates = new Array();
	promoData = new Array();
	var uv, nu, pid, pname;
	if( data.status == "success" ) { 
		if (data.items.length > 0)
		{
			$(data.items).each(function(i, value){
				var myDate=new Date()

				year = value["year"];
				month = value["month"];
				day = value["day"];
				
				if (i == 0)
				{
					$("#promoGeneralContentStartDate").val(year+"/"+month+"/"+day);
				}

				myDate.setFullYear(year,month-1,day);
				promoDates.push(year + "/" + month + "/" + day);
				var promoDetails = value["data"];

				$(promoDetails).each(function(l, dvalue){
					pid = dvalue["pid"];
					pname = dvalue["pname"];
					uv = dvalue["uv"]; //每日使用设备数
					nu = dvalue["nu"]; //新增设备数

					if (typeof(dvalue["uv"])=="undefined")
						uv = 0;
					if (typeof(dvalue["nu"])=="undefined")
						nu = 0;
			
					pi = in_array(pname+" "+pid, promoNames);
					if(pi == -1) {
						promoNames.push(pname+" "+pid);
						pi=promoNames.length-1;
						promoData[pi] = new Array();
						promoData[pi][0] = new Array();
						promoData[pi][1] = new Array();	
					}

					promoData[pi][0].push(uv);
					promoData[pi][1].push(nu);
				});
			});	

			$("#PromoGeneralResultType").show();
			$("#PromoGeneralContentTab0").addClass("selbitem");	
			genTable("PromoGeneralContentTable", promoNames, promoDates, promoData, 0);
			genLineChart("PromoGeneralContentLine","", promoDates, promoNames, promoData, 0);
			$(".tab-content").show();
		}
		else {
			alert("选择的时间区间内无数据");	
			$(".tab-content").hide();

		}

	}
	else {
		alert(data.desc);
	}
	jQuery('#body-wrapper').hideLoading();
}




//tab切换
function changePromoGeneralContentValueType(typeValue) {


	genTable("PromoGeneralContentTable", promoNames, promoDates, promoData, typeValue);
	genLineChart("PromoGeneralContentLine","", promoDates, promoNames, promoData, typeValue);


	$(".bitem").removeClass("selbitem");
	$("#PromoGeneralContentTab"+typeValue).addClass("selbitem");			
	
}

function getPromoGeneralData2() {
	var startDate = new Date();
	var endDate = new Date();

	if ( $("#promoGeneralContentStartDate").val() != "" && $("#promoGeneralContentEndDate").val() != "")
	{
		jQuery('#body-wrapper').showLoading();
		startDate.setFullYear(($("#promoGeneralContentStartDate").val()).substring(0,4), ($("#promoGeneralContentStartDate").val()).substring(5,7)-1,($("#promoGeneralContentStartDate").val()).substring(8,10));
		endDate.setFullYear(($("#promoGeneralContentEndDate").val()).substring(0,4), ($("#promoGeneralContentEndDate").val()).substring(5,7)-1,($("#promoGeneralContentEndDate").val()).substring(8,10));

		days = parseInt((endDate - startDate)/1000/60/60/24)+1;
		if (days > 0)
		{
			var uname = getCookie("uname"); 
			var pwd = encodeURIComponent(getCookie("pwd"));
			var fid = getCookie("uid");

			var APIurl =  "http://10.103.13.15:8000/client/promoAnalytics?fid="+ fid +"&staDate="+$("#promoGeneralContentStartDate").val()+"&pd="+days + "&uname="+uname + "&pwd="+ pwd;
			$.ajax({
				type: "GET",
				url: APIurl,
				cache: true,
				dataType : "jsonp",
				jsonnp: "callback",
				success: loadPromoData,
				error: function(xhr,status,errMsg){
				　　alert(errMsg.code+" "+errMsg.msg);
				}
			});
		}
		else 
			alert("选择时段区间不对!");
	}
	else if ($("#promoGeneralContentStartDate").val() == "" || $("#promoGeneralContentEndDate").val() == "")
	{
		alert("请选择时段!");
	}


//	alert(APIurl);

}